package com.situ.edu.controller;

import com.situ.edu.pojo.entity.Student;
import com.situ.edu.pojo.entity.Xi;
import com.situ.edu.pojo.query.StudentQuery;
import com.situ.edu.service.IXiService;
import com.situ.edu.service.IXiStudentService;
import com.situ.edu.service.impl.XiServiceImpl;
import com.situ.edu.service.impl.XiStudentServiceImpl;
import com.situ.edu.util.MyBeanUtils;
import com.situ.edu.util.PageInfo;
import com.situ.edu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/xiStudent")
public class XiStudentController extends BaseController{

    private IXiStudentService studentService = new XiStudentServiceImpl();
    private IXiService xiService = new XiServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        switch (method){
            case "selectByPage": //查询分页，学生的基本信息：哪个系的
                selectByPage(req,resp);
                break;
            case "selectPage": //学生请假
                selectPage(req,resp);
                break;
            case "updateStatus"://是否批准请假
                updateStatus(req,resp);
                break;
            case "deleteById": //单个删除
                deleteById(req,resp);
                break;
            case "deleteAll"://批量删除
                deleteAll(req,resp);
                break;
            case "add"://添加学生信息：姓名，年龄，性别，院系
                add(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "update":
                update(req,resp);
                break;

        }

    }

    private void selectPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("StudentController.selectPage");
        StudentQuery stuQuery = MyBeanUtils.copyToBean(req,StudentQuery.class);
        PageInfo pageInfo = studentService.selectPage(stuQuery);
        toJSON(resp,pageInfo);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiStudentController.update");
        Student student = MyBeanUtils.copyToBean(req,Student.class);
        studentService.update(student);

        //JSONUtils.toJSON(resp, Result.ok("更新成功"));
        toJSON(resp, Result.ok("更新成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiStudentController.selectById");
        String id = req.getParameter("id");
        Student student = studentService.selectById(Integer.parseInt(id));
        List<Xi> list = xiService.selectAll();
        //JSONUtils.toJSON(resp,Result.ok(blog));
        Map<String,Object> map = new HashMap<>();
        map.put("student",student);
        map.put("list",list);

        toJSON(resp,Result.ok(map));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiStudentController.add");
        Student student = MyBeanUtils.copyToBean(req,Student.class);
        studentService.add(student);
        toJSON(resp,Result.ok("添加成功"));
    }

    /*getParameterValues(String name);是获取前端的多个数据，比如说复选框，*/
    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiStudentController.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        studentService.deleteAll(ids);
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiStudentController.deleteById");
        String id = req.getParameter("id");
        studentService.deleteById(Integer.parseInt(id));
        toJSON(resp,Result.ok("删除成功"));
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiStudentController.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        studentService.updateStatus(Integer.parseInt(id),Integer.parseInt(status));
        toJSON(resp, Result.ok("更新成功"));
    }


    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("StudentController.selectByPage");
        StudentQuery stuQuery = MyBeanUtils.copyToBean(req,StudentQuery.class);
        PageInfo pageInfo = studentService.selectByPage(stuQuery);
        toJSON(resp,pageInfo);
    }
}
